# copyright (c) 2007
# Kazumasa Suzuki

from zope.interface import implements

from twisted.python import usage
from twisted.plugin import IPlugin
from twisted.application.service import IServiceMaker, MultiService
from twisted.application import internet
from nevow import appserver

from midtown import mtsmtp, mtwebtest, mtaxiom, mtchat


class Options(usage.Options):
    optParameters = [["port", "p", 8888, "The port number to listen on."],
                     ["webroot", "r", "/Users/kazu/python/hack/midtown/webroot", "root directory for web docs"]
                     ]

class MidtownServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = "midtown"
    description = "Run this! It'll make your dog happy."
    options = Options

    def makeService(self, options):
        """
        Construct a TCPServer from a factory defined in midtown.
        """
        mm = mtaxiom.MidtownManager()
        master_service = MultiService()
        mta_service = internet.TCPServer(int(options["port"]), 
                                         mtsmtp.MidtownESMTPFactory(mm))
        web_service = internet.TCPServer(8080, 
                                         appserver.NevowSite(mtwebtest.FrontPage(options["webroot"], mm)))
        mta_service.setServiceParent(master_service)
        web_service.setServiceParent(master_service)
        return master_service
        #works for smtp
        #return internet.TCPServer(int(options["port"]), mtsmtp.MidtownESMTPFactory())
        #not tested
        #return internet.TCPServer(int(options["port"]), MidtownFactory())

# Now construct an object which *provides* the relevant interfaces

# The name of this variable is irrelevant, as long as there is *some*
# name bound to a provider of IPlugin and IServiceMaker.

serviceMaker = MidtownServiceMaker()
